<template>
	<view class="goods_item" @click="clickFn">
		<!-- 商品左侧图片区域 -->
		<view class="goods_item_left">
			<radio :checked="item.goods_state" color="#C00000" v-if='showRadio' @click="radioClick"></radio>
			<image class="goods_image" :src="item.goods_small_logo || defaultPic"></image>
		</view>
		<!-- 商品右侧信息区域 -->
		<view class="goods_item_right">
			<!-- 商品标题 -->
			<view class="goods_name">{{item.goods_name}}</view>
			<!-- 商品价格 -->
			<view class="goods_info_box">
				<view class="goods_price">￥{{tofixed}}</view>
				<!-- 商品数量 -->
				<uni-number-box v-if="showNum" :min="1" :value="item.goods_count"
					@change="numChangeHandler"></uni-number-box>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "goods_item",
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png', // 默认的空图片
			};
		},
		props: {
			item: {
				type: Object,
				default: () => ({}),
			},
			showRadio: { //是否显示按钮 
				type: Boolean,
				default: false
			},
			showNum: { // 是否展示价格右侧的 NumberBox 组件
				type: Boolean,
				default: false,
			},
		},
		computed: {
			tofixed() {
				return Number(this.item.goods_price).toFixed(2)
			}
		},
		methods: {
			clickFn() {
				this.$emit('click', this.item)
			},
			radioClick() {
				this.$emit('radio-change', {
					// 商品的 Id
					goods_id: this.item.goods_id,
					// 商品最新的勾选状态
					goods_state: !this.item.goods_state
				})
			},
			numChangeHandler(data){
				this.$emit('num-change',{
					goods_id:this.item.goods_id,
					goods_count:data || 1
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods_item {
		display: flex;
		border-bottom: 1px solid #efefef;
		padding: 8rpx;

		.goods_item_left {
			display: flex;
			justify-content: center;
			align-items: center;

			.goods_image {
				width: 200rpx;
				height: 200rpx;
				vertical-align: middle;
			}
		}

		.goods_item_right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods_name {
				font-size: 24rpx;
			}

			.goods_info_box {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.goods_price {
					font-size: 32rpx;
					color: #c00000;
				}
			}

		}
	}
</style>